/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     textLabel.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "textLabel.h"
#include "bsp.h"
#include "levetop.h"
#include "data.h"
#include "keyBoard.h"

/***********************************************************************************
* Function Name : text_display
* Description   : Show text.
* Input         : - type: text info sources
				  - buff: text info
* Output        : None
* Return        : None
************************************************************************************/
void text_display(uint8_t type, uint8_t *buff)
{
	uint16_t xs, ys, xe, ye; // Display box
	uint8_t Zk_id, w, h;	 // Font ID
	uint8_t encode;			 // encoding type
	uint8_t Alignment, transparent, star;
	uint32_t fcolor, bcolor; // The foreground color of the font, The background color of the font
	uint16_t p_add, p_len, pointer;
	uint32_t zk_addr;
	uint8_t buff1[4] = {0};

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8));
		p_len = buff[7] + (buff[8] << 8);
		xs = buff[9] + (buff[10] << 8);
		ys = buff[11] + (buff[12] << 8);
		xe = buff[13] + (buff[14] << 8);
		ye = buff[15] + (buff[16] << 8);
		w = buff[17];
		h = buff[18];
		Zk_id = buff[19];
		encode = buff[20];
		Alignment = buff[21];
		bcolor = buff[22] + (buff[23] << 8) + (buff[24] << 16);
		fcolor = buff[25] + (buff[26] << 8) + (buff[27] << 16);
		transparent = buff[28] & 0x01;
		star = (buff[28] & 0x02) >> 1;
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		p_len = var[pointer + 3] + (var[pointer + 4] << 8);
		xs = var[pointer + 5] + (var[pointer + 6] << 8);
		ys = var[pointer + 7] + (var[pointer + 8] << 8);
		xe = var[pointer + 9] + (var[pointer + 10] << 8);
		ye = var[pointer + 11] + (var[pointer + 12] << 8);
		w = var[pointer + 13];
		h = var[pointer + 14];
		Zk_id = var[pointer + 15];
		encode = var[pointer + 16];
		Alignment = var[pointer + 17];
		bcolor = var[pointer + 18] + (var[pointer + 19] << 8) + (var[pointer + 20] << 16);
		fcolor = var[pointer + 21] + (var[pointer + 22] << 8) + (var[pointer + 23] << 16);
		transparent = var[pointer + 24] & 0x01 ;
		star = (var[pointer + 24] & 0x02) >> 1;
	}

	Var_AddrRemapping(&p_add);
	p_add = p_add*2;

	if (Pop_up_flag == 1) // Popup offset
	{
		xs += Pop_up_x;
		ys += Pop_up_y;
		xe += Pop_up_x;
		ye += Pop_up_y;
	}

	LT_ReadFlash_UI(buff1, addr_index[2] + Zk_id * 8, 4);
	zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

	// display
	LT_Print_zk_Font(encode, zk_addr,
						0, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, PIC_ON_BaseImg, PICINFO[0], star,
						xs, ys, xe, ye,
						0, 0, &var[p_add], p_len * 2);
}

void text_display_to_buff(uint8_t type, uint8_t *buff,uint16_t overlay_x, uint16_t overlay_y, uint16_t overlay_w,uint16_t overlay_h)
{
	uint16_t xs, ys, xe, ye; // Display box
	uint8_t Zk_id, w, h;	 // Font ID
	uint8_t encode;			 // encoding type
	uint8_t Alignment, transparent, star;
	uint32_t fcolor, bcolor; // The foreground color of the font, The background color of the font
	uint16_t p_add, p_len, pointer;
	uint32_t zk_addr;
	uint8_t buff1[4] = {0};
	uint8_t lay_flag;

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8));
		p_len = buff[7] + (buff[8] << 8);
		xs = buff[9] + (buff[10] << 8);
		ys = buff[11] + (buff[12] << 8);
		xe = buff[13] + (buff[14] << 8);
		ye = buff[15] + (buff[16] << 8);
		w = buff[17];
		h = buff[18];
		Zk_id = buff[19];
		encode = buff[20];
		Alignment = buff[21];
		bcolor = buff[22] + (buff[23] << 8) + (buff[24] << 16);
		fcolor = buff[25] + (buff[26] << 8) + (buff[27] << 16);
		transparent = buff[28] & 0x01;
		star = (buff[28] & 0x02) >> 1;
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		p_len = var[pointer + 3] + (var[pointer + 4] << 8);
		xs = var[pointer + 5] + (var[pointer + 6] << 8);
		ys = var[pointer + 7] + (var[pointer + 8] << 8);
		xe = var[pointer + 9] + (var[pointer + 10] << 8);
		ye = var[pointer + 11] + (var[pointer + 12] << 8);
		w = var[pointer + 13];
		h = var[pointer + 14];
		Zk_id = var[pointer + 15];
		encode = var[pointer + 16];
		Alignment = var[pointer + 17];
		bcolor = var[pointer + 18] + (var[pointer + 19] << 8) + (var[pointer + 20] << 16);
		fcolor = var[pointer + 21] + (var[pointer + 22] << 8) + (var[pointer + 23] << 16);
		transparent = var[pointer + 24] & 0x01 ;
		star = (var[pointer + 24] & 0x02) >> 1;
	}

	Var_AddrRemapping(&p_add);
	p_add = p_add*2;

	if (Pop_up_flag == 1) // Popup offset
	{
		xs += Pop_up_x;
		ys += Pop_up_y;
		xe += Pop_up_x;
		ye += Pop_up_y;
	}

	LT_ReadFlash_UI(buff1, addr_index[2] + Zk_id * 8, 4);
	zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

	LT_Print_zk_Font_BUFF(encode, zk_addr,
						  0, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, overlay_w, overlay_h, mix_ram,
						  xs - overlay_x, ys - overlay_y, xe - xs + 1, ye - ys + 1,
						  0, 0, &var[p_add], p_len * 2);
}
